package bos.web.action;



import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import bos.domain.Workordermanage;
import bos.service.WorkordermanageService;
import bos.utils.BOSContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Controller
@Scope("prototype")
public class TaskAction extends ActionSupport{
	@Autowired
	private TaskService taskService;
	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private WorkordermanageService workordermanageService;
	
	
	public String findGroupTask(){
		TaskQuery query = taskService.createTaskQuery();
		
		String id = BOSContext.getLoginUser().getId();
		
		query.taskCandidateUser(id);
		
		List<Task> list=query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "grouplist";
	}
	
	
	private String taskId;
	
	public String getTaskId() {
		return taskId;
	}

	public void setTaskId(String taskId) {
		this.taskId = taskId;
	}

	public String showData() throws IOException{
		Map<String,Object> variables=taskService.getVariables(taskId);
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(variables.toString());
		
		return NONE;
	}
	
	public String takeTask(){
		String userId = BOSContext.getLoginUser().getId();
		taskService.claim(taskId, userId);
		
		return "togrouptasklist";
	}
	public String findPersonalTask(){
		TaskQuery query = taskService.createTaskQuery();
		
		String assignee = BOSContext.getLoginUser().getId();
		
		query.taskAssignee(assignee);
		
		List<Task> list=query.list();
		ActionContext.getContext().getValueStack().set("list", list);
		return "personallist";
		
	}
	private Integer check;
	
	
	public String checkWorkOrderManage(){
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		
		String processInstanceId = task.getProcessInstanceId();
		
		ProcessInstance processInstance = runtimeService.createProcessInstanceQuery()
				.processInstanceId(processInstanceId).singleResult();
		String workordermanageId = processInstance.getBusinessKey();
		
		Workordermanage workordermanage=workordermanageService.findById(workordermanageId);
		if(check==null){
			
			ActionContext.getContext().getValueStack().set("map",workordermanage);
			return "check";
			
		}else{
			
			workordermanageService.checkWorkorderManage(taskId,check,workordermanageId);
			
			return "topersonaltasklist";
		}
		
	}

	public Integer getCheck() {
		return check;
	}

	public void setCheck(Integer check) {
		this.check = check;
	}
	
	public String outStore(){
		taskService.complete(taskId);
		return "topersonaltasklist";
	}
	
	public String receive(){
		taskService.complete(taskId);
		return "topersonaltasklist";
	}
	public String transferGoods(){
		taskService.complete(taskId);
		return "topersonaltasklist";
	}
}
